﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_client.csharp.model;
using mars_client.csharp.util;
using mars_client.gui.page;
using System.Windows.Controls;
using System.Windows;
using mars_client.csharp.model.vo;
using mars_client.gui.window;
using mars_client.csharp.vo;
using mars_client.csharp.enums;
using mars_client.csharp.cache;
using mars_client.csharp.radar;

namespace mars_client.csharp.ao.impl
{
    class MyFriendsAOImpl : MyFriendsAO
    {
        private MyFriendsAOImpl() { }
        private readonly static MyFriendsAOImpl instance = new MyFriendsAOImpl();
        internal static MyFriendsAOImpl Instance
        {
            get
            {
                return instance;
            }
        }
        GuiCache guic = GuiCache.Instance;
        // 自己的朋友
        public void setMineFriends(List<UserVO> userVOs)
        {
            MyFriendsPage page = (MyFriendsPage)guic.getGui("MyFriendsPage");
            page.Dispatcher.Invoke(() =>
            {
                page.friendsWrapper.Children.Clear();
                for (int i = 0; i< userVOs.Count;i++)
                {
                    userVOs[i].nickName += userVOs[i].isOnline == 1 ? "(在线)" : "(离线)";
                    page.friendsWrapper.Children.Add(RenderUtil.generateFriendTemp(userVOs[i],i));
                }
            });
        }
        
        public void setFoundedFriends(List<UserVO> userVOs)
        {
            AddFriendWindow addFriendWindow = (AddFriendWindow)guic.getGui("AddFriendWindow");
            App.Current.Dispatcher.Invoke(() =>
            {
                addFriendWindow.Dispatcher.Invoke(() =>
                {
                    if (addFriendWindow != null)
                    {
                        addFriendWindow.Close();
                        guic.removeGui("AddFriendWindow");
                    }
                });
                UsersWindow usersWin = new UsersWindow();
                usersWin.Dispatcher.Invoke(() =>
                {
                    for (int i = 0; i < userVOs.Count; i++)
                    {
                        usersWin.usersWrapper.Children.Add(RenderUtil.generateUserTemp(userVOs[i],"visible"));
                    }
                    usersWin.ShowDialog();
                });
            });
            
        }

        public void notify(FriendNotifyVO friendNotifyVO)
        {
            App.Current.Dispatcher.Invoke(() =>
            {
                if(friendNotifyVO.isAgree == (int)RequestEnum.isAgree.launch && friendNotifyVO.receiverVO.id == UserCache.Instance.userVO.id)
                {
                    VerifyWindow verifyWindow = new VerifyWindow();
                    verifyWindow.Dispatcher.Invoke(() =>
                    {
                        verifyWindow.friendName.Content = friendNotifyVO.senderVO.nickName;
                        verifyWindow.friendSex.Content = friendNotifyVO.senderVO.sex;
                        verifyWindow.attchInfo.Content = friendNotifyVO.memo;
                        verifyWindow.headImg.Source = SysUtil.netImageSource(friendNotifyVO.senderVO.headImg);
                        verifyWindow.disagreeBlock.Uid = friendNotifyVO.senderVO.id.ToString();
                        verifyWindow.agreeBlock.Uid = friendNotifyVO.senderVO.id.ToString();
                        verifyWindow.Show();
                    });
                }
                else if (friendNotifyVO.isAgree == (int)RequestEnum.isAgree.disagree)
                {
                    //MessageBox.Show(friendNotifyVO.receiverVO.nickName + " 拒绝你的好友请求");
                    LogUtil.console(friendNotifyVO.receiverVO.nickName + " 拒绝你的好友请求");
                    // 刷新列表
                    MessageCO messageCO = new MessageCO();
                    messageCO.type = (int)RequestEnum.request.GetFriends;
                    Radar.Instance.sendData(messageCO);
                }
                else if(friendNotifyVO.isAgree == (int)RequestEnum.isAgree.disagree)
                {
                    //MessageBox.Show(friendNotifyVO.receiverVO.nickName + " 接受你的好友请求");
                    LogUtil.console(friendNotifyVO.receiverVO.nickName + " 接受你的好友请求");
                    // 刷新列表
                    MessageCO messageCO = new MessageCO();
                    messageCO.type = (int)RequestEnum.request.GetFriends;
                    Radar.Instance.sendData(messageCO);
                }
            });
        }

        public void deleteFriend(UserVO userVO)
        {
            MessageBox.Show("好友 "+userVO.nickName+" 已删除！");
            LogUtil.console("好友 " + userVO.nickName + " 已删除！");
            MessageCO messageCO = new MessageCO();
            messageCO.type = (int)RequestEnum.request.GetFriends;
            Radar.Instance.sendData(messageCO);
        }
    }
}
